package com.sdwz.user.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdwz.commonutil.Result;
import com.sdwz.serviceutil.utils.JwtUtil;
import com.sdwz.user.entity.BorrowCardInfo;
import com.sdwz.user.entity.UserInfo;
import com.sdwz.user.entity.vo.AuthVo;
import com.sdwz.user.service.BorrowCardInfoService;
import com.sdwz.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class BorrowCardApiController {

    @Autowired
    BorrowCardInfoService borrowCardInfoService;

    @Autowired
    UserInfoService userInfoService;

    /**
     * 获取借阅卡信息
     * @param request
     * @return
     */
    @GetMapping("/auth/borrow/getCardInfo")
    public Result getCardInfo(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        String token = request.getHeader("token");
        String userId = String.valueOf(JwtUtil.getUserId(token));

        UserInfo userInfo = userInfoService.getById(userId);
        Integer status = userInfo.getStatus();
        result.put("status",status);
        if (status == 0 || status == 1||status == -1){
            return Result.ok(result);
        }

        QueryWrapper<BorrowCardInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        BorrowCardInfo borrowCardInfo = borrowCardInfoService.getOne(wrapper);
        borrowCardInfo.setCertificatesUrl(null);
        result.put("card",borrowCardInfo);

        return Result.ok(result);

    }

}
